<?php
class Http {
	// post请求
	public function doPost($url, $data, $httpOpts = null) {
		$options = array (
				CURLOPT_POST => TRUE,
				CURLOPT_CUSTOMREQUEST => 'POST',
				// 传递一个作为HTTP “POST”操作的所有数据的字符串。
				CURLOPT_POSTFIELDS => $data 
		);
		// 设置请求额外参数
		if (is_array ( $httpOpts )) {
			foreach ( $httpOpts as $key => $value ) {
				$options [$key] = $value;
			}
		}
		return $this->doGet ( $url, $options );
	}
	
	// get请求
	public function doGet($url, $httpOpts = null) {
		if (! isset ( $url ) || empty ( $url )) {
			$rsp = array (
					'code' => 400,
					'message' => '缺少请求链接' 
			);
			return json_encode ( $rsp );
		}
		// 创建了一个curl会话资源
		$curl_handler = curl_init ();
		// 设置请求默认参数
		$options = array (
				CURLOPT_URL => $url, // 请求地址
				CURLOPT_RETURNTRANSFER => 1, // 设置是否将响应结果存入变量，1是存入，0是直接echo输出
				CURLOPT_CONNECTTIMEOUT => 60 // 超时时间60秒
		);
		// 解析链接，判断请求是https协议
		if (0 === strpos ( strtolower ( $url ), 'https' )) {
			$options [CURLOPT_SSL_VERIFYPEER] = 0; // 对认证证书来源的检查
			$options [CURLOPT_SSL_VERIFYHOST] = 0; // 从证书中检查SSL加密算法是否存在
			$options [CURLOPT_SSLVERSION] = 1;
		}
		
		// 设置请求额外参数
		if (is_array ( $httpOpts )) {
			foreach ( $httpOpts as $key => $value ) {
				$options [$key] = $value;
			}
		}
		// 设置请求整体参数
		curl_setopt_array ( $curl_handler, $options );
		
		// 执行curl，然后将响应结果存入$curl_result变量
		$curl_result = curl_exec ( $curl_handler );
		
		// http请求错误码
		$curl_http_status = curl_getinfo ( $curl_handler, CURLINFO_HTTP_CODE );
		// http请求信息
		$curl_http_info = curl_getinfo ( $curl_handler );
	
		if ($curl_result === false) {
			$error = curl_error ( $curl_handler );
			// 关闭curl会话资源
			curl_close ( $curl_handler );
			$rsp = array (
					'code' => $curl_http_status,
					'message' => $error,
					'http_info' => $curl_http_info 
			);
			// 将结果转成json格式
			return json_encode ( $rsp );
		}
		
		// 关闭curl会话资源
		curl_close ( $curl_handler );
		
		if ($curl_http_status == 0 || ($curl_http_status >= 200 && $curl_http_status < 300)) {
			return $curl_result;
		}
	}
	
	// 执行http请求，并获取响应头
	public function doGetWithResponseHeader($url, $httpOpts = null) {
		if (! isset ( $url ) || empty ( $url )) {
			$rsp = array (
					'code' => 400,
					'message' => '缺少请求链接'
			);
			return json_encode ( $rsp );
		}
		// 创建了一个curl会话资源
		$curl_handler = curl_init ();
		// 设置请求默认参数
		$options = array (
				CURLOPT_URL => $url, // 请求地址
				CURLOPT_RETURNTRANSFER => 1, // 设置是否将响应结果存入变量，1是存入，0是直接echo输出
				CURLOPT_CONNECTTIMEOUT => 60 // 超时时间60秒
		);
		// 解析链接，判断请求是https协议
		if (0 === strpos ( strtolower ( $url ), 'https' )) {
			$options [CURLOPT_SSL_VERIFYPEER] = 0; // 对认证证书来源的检查
			$options [CURLOPT_SSL_VERIFYHOST] = 0; // 从证书中检查SSL加密算法是否存在
			$options [CURLOPT_SSLVERSION] = 1;
		}
	
		$options [CURLOPT_HEADER] = true; // 返回response_header
	
		// 设置请求额外参数
		if (is_array ( $httpOpts )) {
			foreach ( $httpOpts as $key => $value ) {
				$options [$key] = $value;
			}
		}
		// 设置请求整体参数
		curl_setopt_array ( $curl_handler, $options );
	
		// 执行curl，然后将响应结果存入$curl_result变量
		$curl_result = curl_exec ( $curl_handler );
	
		// http请求错误码
		$curl_http_status = curl_getinfo ( $curl_handler, CURLINFO_HTTP_CODE );
		// 获得响应结果里的：头大小
		$headerSize = curl_getinfo($curl_handler, CURLINFO_HEADER_SIZE);
		// 根据头大小去获取头信息内容
		$header = substr($curl_result, 0, $headerSize);
	
		// 关闭curl会话资源
		curl_close ( $curl_handler );
		return $header;
		
	}
	
	
	//put请求
	public function doPut( $url, $data, $httpOpts = null ) {
		$options = array (
				CURLOPT_CUSTOMREQUEST => 'PUT',
				CURLOPT_POSTFIELDS => $data
		);
		// 设置请求额外参数
		if (is_array ( $httpOpts )) {
			foreach ( $httpOpts as $key => $value ) {
				$options [$key] = $value;
			}
		}
		return $this->doGet ( $url, $options );
	
	}
	
	//执行put请求，并返回响应头
	public function doPutWithResponseHeader( $url, $data, $httpOpts = null ) {
		$options = array (
				CURLOPT_CUSTOMREQUEST => 'PUT',
				CURLOPT_POSTFIELDS => $data
		);
		// 设置请求额外参数
		if (is_array ( $httpOpts )) {
			foreach ( $httpOpts as $key => $value ) {
				$options [$key] = $value;
			}
		}
		return $this->doGetWithResponseHeader ( $url, $options );
	
	}

}
?>
